<script setup lang="ts">
import type { VbenFormProps } from '@vben/common-ui';
import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { RowType } from './sysDept.data';

import { Page, useVbenModal } from '@vben/common-ui';

import { message } from 'ant-design-vue';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { sysDeptDelApi, sysDeptQueryListApi } from '#/api/system/sysDept';
import { queryDictOptionsApi, useDictionaries } from '#/utils';

import SysDeptFormModal from './modules/SysDeptFormModal.vue';

const { translateValue } = useDictionaries(['status']);

const formOptions: VbenFormProps = {
  layout: 'horizontal',
  wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3',
  showCollapseButton: false,
  actionButtonsReverse: false,
  showDefaultActions: true,
  submitOnChange: false,
  submitOnEnter: false,
  commonConfig: {
    colon: true,
    labelWidth: 80,
  },
  schema: [
    {
      component: 'Input',
      label: '部门名称',
      fieldName: 'deptName',
      componentProps: {
        placeholder: '请输入部门名称',
      },
    },
    {
      component: 'ApiSelect',
      label: '状态',
      fieldName: 'status',
      componentProps: {
        placeholder: '请选择状态',
        allowClear: true,
        showSearch: true,
        afterFetch: (data) => {
          return data.map((item: any) => ({
            label: item.itemText,
            value: Number.parseInt(item.itemVal),
          }));
        },
        api: queryDictOptionsApi,
        params: {
          dictCode: 'status',
        },
      },
    },
  ],
};

const gridOptions: VxeTableGridOptions<RowType> = {
  height: 'auto',
  pagerConfig: {
    enabled: false,
  },
  rowConfig: {
    keyField: 'id',
  },
  treeConfig: {},
  toolbarConfig: {
    slots: {
      buttons: 'toolbar-buttons',
    },
  },
  columns: [
    { title: '序号', type: 'seq', width: 50, fixed: 'left', align: 'center' },
    {
      title: '部门名称',
      field: 'deptName',
      align: 'center',
      treeNode: true,
      minWidth: 160,
    },
    {
      title: '负责人',
      field: 'leader',
      align: 'center',
      minWidth: 80,
    },
    {
      title: '联系电话',
      field: 'phone',
      align: 'center',
      minWidth: 80,
    },
    {
      title: '邮箱',
      field: 'email',
      align: 'center',
      visible: false,
    },
    {
      title: '描述',
      field: 'description',
      align: 'center',
      minWidth: 130,
    },
    {
      title: '排序',
      field: 'sortRank',
      align: 'center',
      minWidth: 50,
    },
    {
      title: '状态',
      field: 'status',
      align: 'center',
      cellRender: {
        name: 'CellDict',
        attrs: {
          dictCode: 'status',
          translateValue,
        },
      },
      minWidth: 80,
    },
    {
      title: '祖先编码',
      field: 'ancestorIds',
      align: 'center',
      visible: false,
    },
    {
      title: '主键',
      field: 'id',
      align: 'center',
      visible: false,
    },
    {
      title: '父级部门',
      field: 'pid',
      align: 'center',
      visible: false,
    },
    {
      title: '是否叶子节点',
      field: 'isLeaf',
      align: 'center',
      visible: false,
    },
    {
      title: '创建人',
      field: 'createBy',
      align: 'center',
      visible: false,
    },
    {
      title: '创建时间',
      field: 'createTime',
      align: 'center',
      minWidth: 160,
    },
    {
      title: '更新人',
      field: 'updateBy',
      align: 'center',
      visible: false,
    },
    {
      title: '更新时间',
      field: 'updateTime',
      align: 'center',
      visible: false,
    },
    {
      title: '操作',
      field: 'action',
      align: 'center',
      minWidth: 160,
      fixed: 'right',
      slots: {
        default: 'action',
      },
    },
  ],
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues) => {
        return await sysDeptQueryListApi({
          pageNo: page.currentPage,
          pageSize: page.pageSize,
          ...formValues,
        });
      },
    },
  },
};
const [Grid, gridApi] = useVbenVxeGrid({ formOptions, gridOptions });
const [FromModal, formModalApi] = useVbenModal({
  connectedComponent: SysDeptFormModal,
  onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      gridApi.query();
    }
  },
});
async function handlerAdd() {
  formModalApi.setData({ id: undefined });
  formModalApi.open();
}
async function handlerAddChild(row) {
  formModalApi.setData({ id: undefined, pid: row.id });
  formModalApi.open();
}
async function handlerEdit(row) {
  formModalApi.setData({ id: row.id });
  formModalApi.open();
}
async function handlerDel(row) {
  const msg = await sysDeptDelApi(row.id);
  message.success(msg);
  await gridApi.query();
}
async function handlerExpandAll() {
  gridApi.grid?.setAllTreeExpand(true);
}
async function handlerCollapseAll() {
  gridApi.grid?.setAllTreeExpand(false);
}
</script>

<template>
  <Page auto-content-height>
    <FromModal class="w-[50%]" />
    <Grid>
      <template #toolbar-buttons>
        <a-button
          v-access:code="'sys:dept:add'"
          type="primary"
          class="mr-2"
          @click="handlerAdd()"
        >
          新增
        </a-button>
      </template>
      <template #toolbar-tools>
        <a-button
          type="primary"
          class="mr-2"
          danger
          @click="handlerExpandAll()"
        >
          展开全部
        </a-button>
        <a-button
          type="primary"
          class="mr-2"
          danger
          @click="handlerCollapseAll()"
        >
          折叠全部
        </a-button>
      </template>
      <template #action="{ row }">
        <a-button
          v-access:code="'sys:dept:child'"
          type="link"
          size="small"
          @click="handlerAddChild(row)"
        >
          新增下级
        </a-button>
        <a-button
          v-access:code="'sys:dept:edit'"
          type="link"
          size="small"
          @click="handlerEdit(row)"
        >
          编辑
        </a-button>
        <a-popconfirm
          cancel-text="再想想"
          title="您确定要删除吗?"
          @confirm="handlerDel(row)"
        >
          <a-button
            v-access:code="'sys:dept:del'"
            type="link"
            danger
            size="small"
          >
            删除
          </a-button>
        </a-popconfirm>
      </template>
    </Grid>
  </Page>
</template>
